<template>
  <el-popover
    ref="pop"
    placement="right"
    :title="iconClass"
    width="300"
    :show-arrow="false"
    trigger="click">
    <div class="icon-group">
      <svg-icon v-if="isSvgIcon" :name="iconClass"></svg-icon>
      <i v-else :class="'fa fa-' + iconClass"></i>
    </div>
    <el-row :gutter="10">
      <el-col :span="8">
        <el-tooltip :append-to-body="false" :content="iconClass" placement="top">
          <el-button v-clipboard="iconClass" style="width: 100%;">Class</el-button>
        </el-tooltip>
      </el-col>
      <el-col v-if="!isSvgIcon" :span="8">
        <el-tooltip :append-to-body="false" :content="iconHtml" placement="top">
          <el-button v-clipboard="iconHtml" style="width: 100%;">HTML</el-button>
        </el-tooltip>
      </el-col>
      <el-col :span="8">
        <el-tooltip :append-to-body="false" :content="iconComponent" placement="top">
          <el-button v-clipboard="iconComponent" style="width: 100%;">组件</el-button>
        </el-tooltip>
      </el-col>
    </el-row>
    <template #reference>
      <svg-icon v-if="isSvgIcon" :name="iconClass"></svg-icon>
      <span v-else>
        <el-tag type="info" class="icon-tag">
          <i :class="'fa fa-' + iconClass"></i>
        </el-tag>
        <span style="font-size: 10px;">{{iconClass}}</span>
      </span>
    </template>
  </el-popover>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue';
import {
  ElPopover,
} from 'element-plus';

export default defineComponent({
  name: 'IconCell',
  components: {
    [ElPopover.name]: ElPopover,
  },
  props: {
    name: {
      type: String,
      required: false,
    },
    icon: {
      type: String,
      required: false,
    },
    type: String,
  },
  setup(props) {
    const isSvgIcon = computed<boolean>(() => props.type === 'svg');
    const iconClass = computed<string>(() => (props.name || props.icon || 'font-awesome'));
    const iconHtml = computed<string>(() => {
      if (!isSvgIcon.value) {
        return `<i class="fa fa-${iconClass.value}" aria-hidden="true" />`;
      }
      return '';
    });
    const iconComponent = computed<string>(() => {
      if (!isSvgIcon.value) {
        return `<fa-icon name="${iconClass.value}" />`;
      }
      return `<svg-icon icon-name="${iconClass.value}" />`;
    });
    return {
      isSvgIcon,
      iconClass,
      iconHtml,
      iconComponent,
    };
  },
});
</script>

<style lang="less" scoped>
.icon-group {
  text-align: center;
  font-size: 200px;
  border-top: 1px solid #dcdfe6;
}
.icon-tag {
  width: 32px;
  text-align: center;
  margin-right: 5px;
}
</style>
